package compiler;

import tester.ExeRunner;
import tester.Config;

import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-28
 * Time: 18:32:39
 * To change this template use File | Settings | File Templates.
 */
public class Compiler {
  private static String gccPath;
  private static String includePath;
  private static String libPath;
  private static File gccPathFile;

  static {
    Properties gccConfig = new Properties();
    try {
      gccConfig.load(new FileInputStream(Config.getGccConfigFile()));
      gccPath = gccConfig.getProperty("gcc.path");
      includePath = gccConfig.getProperty("include.path");
      libPath = gccConfig.getProperty("lib.path");
    } catch (IOException e) {
      gccPath = null;
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    gccPathFile = new File(gccPath);
  }

  private static String getExtension(String fileName) {
    int index = fileName.lastIndexOf('.');
    if (index == -1)
      return "";
    else
      return fileName.substring(index + 1).toUpperCase();
  }

  public static void compile(File sourceFile) throws IOException {
    if (gccPath == null) return; // if error with compiler
    if (!sourceFile.exists()) return;
    String ext = getExtension(sourceFile.getName());
    if (ext.equals("C")) {
      compileC(sourceFile);
    } else if (ext.equals("CPP")) {
      compileCpp(sourceFile);
    }
  }

  private static void compileCpp(File sourceFile) throws IOException {
    String fileName = sourceFile.getName();
    int li = fileName.lastIndexOf('.');
    String name = fileName.substring(0, li);
    File path = new File(sourceFile.getParentFile().getAbsolutePath());
    String[] commandLines = {
        gccPath + "\\g++.exe",
        "\"" + sourceFile.getAbsolutePath() + "\"",
        "-o", "\"" + path + File.separator + name + ".exe\"",
        "-pg", "-g3",
        "-I", "\"" + includePath + "\\c++\\3.3.1\"",
        "-I", "\"" + includePath + "\\c++\\3.3.1\\mingw32\"",
        "-I", "\"" + includePath + "\\c++\\3.3.1\\backward\"",
        "-I", "\"" + libPath + "\\gcc-lib\\mingw32\\3.3.1\\include\"",
        "-I", "\"" + includePath + "\"",
        "-L", "\"" + libPath + "\"",
        "-lgmon", "-pg", "-g3"
    };
    ExeRunner.executeNoIO(gccPathFile, Config.COMPILE_TIMEOUT, commandLines);
  }

  private static void compileC(File sourceFile) throws IOException {
    String fileName = sourceFile.getName();
    int li = fileName.lastIndexOf('.');
    String name = fileName.substring(0, li);
    File path = new File(sourceFile.getParentFile().getAbsolutePath());
    String[] commandLines = {
        gccPath + "\\gcc.exe",
        "\"" + sourceFile.getAbsolutePath() + "\"",
        "-o", "\"" + path + File.separator + name + ".exe\"",
        "-pg", "-g3",
        "-I", "\"" + includePath + "\"",
        "-L", "\"" + libPath + "\"",
        "-lgmon", "-pg", "-g3"
    };
    ExeRunner.executeNoIO(gccPathFile, Config.COMPILE_TIMEOUT, commandLines);
  }
}

/* command line for compiling C
          "\"" + gccPath + "\\gcc.exe\" " +
          "\"" + sourceFile.getAbsoluteFile() + "\" -o " +
          "\"" + path + File.separator + name + ".exe\"" +
          " -pg -g3 -I \"" + includePath + "\"" +
          " -L \"" + libPath + "\"" + " -lgmon -pg  -g3";
*/

/* command line for compiling CPP
          "\"" + gccPath + "\\g++.exe\" " +
          "\"" + sourceFile.getAbsoluteFile() + "\" -o " +
          "\"" + path + File.separator + name + ".exe\"" +
          " -pg -g3 -I \"" + includePath + "\\c++\\3.3.1\"" +
          " -I \"" + includePath + "\\c++\\3.3.1\\mingw32\"" +
          " -I \"" + includePath + "\\c++\\3.3.1\\backward\"" +
          " -I \"" + libPath + "\\gcc-lib\\mingw32\\3.3.1\\include\"" +
          " -I \"" + includePath + "\"" +
          " -L \"" + libPath + "\"" +
          " -lgmon -pg  -g3";
*/